//
//  NongViewController.m
//  植物管理
//
//  Created by liujianlong on 2022/11/22.
//

#import "NongViewController.h"

@interface NongViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collection;
@property (strong, nonatomic) IBOutlet CCTextField *input;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, assign) NSInteger index;

@end

@implementation NongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)initUI{
    self.title = LS(@"记农事");
    self.array = @[@"施肥",@"浇水",@"除草",@"用药",@"翻耕",@"除虫",@"其他农事"];
    self.index = 1000;
    for (int i = 0; i<self.array.count; i++) {
        NSString *str = self.array[i];
        if ([self.model.job isEqualToString:str]) {
            self.index = i;
        }
    }
    self.input.text = self.model.times;
    [self.rightBtn setTitle:LS(@"保存") forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[@"#3E6F51" color] forState:UIControlStateNormal];
    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (void)rightBtnAction{
    if (self.index >= self.array.count) {
        return [CCHud msg:LS(@"请选择一个农事")];
    }else if (self.input.text.length <= 0){
        return [CCHud msg:LS(@"次数不对")];
    }else if ([self.input.text isNumber] == NO){
        return [CCHud msg:LS(@"次数不对")];
    }
    self.model.job = self.array[self.index];
    self.model.times = self.input.text;
    [self backBtnAction];
    if (self.block) {
        self.block();
    }
}
#pragma mark - UICollectionViewDataSource
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (![string isEqualToString:@""] && ![string isNumber]) {
        return NO;
    }
    return YES;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 7;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UILabel *lbl = [UILabel new];
    lbl.minimumScaleFactor = 12;
    lbl.bgHexColor = self.index == indexPath.row ? @"#3E6F51" : @"#C4E1CB";
    lbl.textHexColor = self.index == indexPath.row ? @"#FFFFFF" : @"#3E6F51";
    lbl.text = LS(self.array[indexPath.row]);
    lbl.font = [UIFont systemFontOfSize:14];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.cornerRadius = 10;
    [cell.contentView addSubview:lbl];
    lbl.c_edges.equalTo(cell.contentView);
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.index = indexPath.row;
    [collectionView reloadData];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 40 - 14 * 5) / 4.0;
    return  CGSizeMake(w, 33);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(14, 14, 14, 14);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
@end
